/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: 联动优势科技有限公司</p>
 * <p>Aug 13, 2012 2:42:31 PM</p>
 * @author wangyunpeng
 * @version 1.0
 */
package com.ce.study.procum;

import java.util.LinkedList;

/** 
 * desc:
 * <p>创建人：wangyunpeng 创建日期：Aug 13, 2012 </p>
 * @version V1.0  
 */
public class GoodsStock{
	private LinkedList<String> stock = new LinkedList<String>();
	
	public synchronized void addGoods(String goods) {
		while(stock.size() >= 10) { //容量限制为100,如果大于100,调用此方法的线程处于等待状态
			try {
				wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		stock.add(goods);
		System.out.println("add goods is:["+goods+"],(size:"+stock.size()+")");
		notifyAll();
	}
	
	public synchronized String getGoods() {
		while(stock.size() <= 0) { //容量为空，则所有获取商品的线程处于等待
			try {
				wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		String goods = stock.removeFirst();
		System.out.println("get goods is:["+goods+"],(size:"+stock.size()+")");
		notifyAll();
		return goods;
	}
}
